home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
dtk_demo.zip
/
COMPARE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-09-12
|
4KB
|
164 lines
/* COMPARE.C
* multiple file comparison utility
* last mod.: 1-AUG-91
*/
#include <STDIO.H>
#include <FCNTL.H>
#include <SYS\TYPES.H>
#include <SYS\STAT.H>
#include <IO.H>
#include <STDLIB.H>
#include <STRING.H>
#include <L_FILE.H>
#include <L_DIR.H>
#define MSC TRUE /* set to FALSE for Turbo C/C++ */
#if MSC
#include <MALLOC.H>
#endif
char *usage = "\nUse: COMPARE file_1 file_2"
"\n wildcards OK (e.g. COMPARE *.TXT *.DEC)\n";
char path1[_MAX_PATH_];
char path2[_MAX_PATH_];
char drive1[_MAX_DRIVE_];
char drive2[_MAX_DRIVE_];
char dir1[_MAX_PATH_];
char dir2[_MAX_PATH_];
char file1[_MAX_FILE_];
char file2[_MAX_FILE_];
Str_ptr buff1 = NULL;
Str_ptr buff2 = NULL;
long offset;
Uint buffsize;
void main (int argc, char **argv);
int compare_files(Str_ptr file1, Str_ptr file2);
/*---------------------------*/
void main(int argc,char **argv)
{
int i, n, err_flag;
File *files_1;
File *files_2;
if ( argc < 3 )
{
printf(usage);
exit(0);
}
for ( i=0; i<argc; i++ )
strupr(argv[i]);
split_path(argv[1],drive1,dir1,NULL,NULL);
split_path(argv[2],drive2,dir2,NULL,NULL);
n = get_input_output_files(argc,argv,&files_1,&files_2,NULL,&err_flag);
/* this function allocates 2*n*sizeof(File) bytes from
* the near heap to hold the two File arrays
*/
if ( err_flag )
{
switch ( err_flag )
{
case -1: printf("Not enough command line parameters.\n"); break;
case -2: printf("Invalid command line parameters.\n"); break;
case -3: printf("Out of near heap space.\n"); break;
case -4: break; /* ignore this error condition */
default: printf("Unknown error.\n");
}
}
else if ( !n )
printf("No files found matching %s.\n",argv[1]);
else
{
#if MSC
buffsize = _memmax();
#else
buffsize = coreleft();
#endif
buffsize = ( buffsize - 128 ) / 2;
if ( ( buff1 = malloc(buffsize) ) != NULL )
buff2 = malloc(buffsize);
if ( buff2 == NULL )
printf("Insufficient memory in near heap.\n");
else
{
printf("Files matching %s: %d\n",argv[1],n);
for ( i=0; i<n; i++ )
{
printf("%12s %12s ",files_1[i].name,files_2[i].name);
make_path(path1,drive1,dir1,files_1[i].name);
make_path(path2,drive2,dir2,files_2[i].name);
switch ( compare_files(path1,path2) )
{
case 0: printf("Files are the same.\n"); break;
case -1: printf("First file not found.\n"); break;
case -2: printf("Second file not found.\n"); break;
case -3: printf("Read error with first file.\n"); break;
case -4: printf("Read error with second file.\n"); break;
case -5: printf("Files have different sizes.\n"); break;
case -6: printf("Files differ at offset 0x%lX.\n",offset);
}
}
}
}
}
/*----------------------------*/
int compare_files(Str_ptr file1,
Str_ptr file2)
{
int bytes_read, fh1, fh2, error=0;
Uint i, block = 0;
if ( ( fh1 = open(file1,O_RDONLY|O_BINARY) ) == -1 )
return ( -1 );
if ( ( fh2 = open(file2,O_RDONLY|O_BINARY) ) == -1 )
{
close(fh1);
return ( -2 );
}
if ( filelength(fh1) != filelength(fh2) )
error = -5;
else
{
while ( !eof(fh1) )
{
if ( ( bytes_read = read(fh1,buff1,buffsize) ) == -1 )
{
error = -3;
break;
}
if ( read(fh2,buff2,bytes_read) < bytes_read )
{
error = -4;
break;
}
if ( memcmp(buff1,buff2,bytes_read) )
{
i=0;
while ( buff1[i] == buff2[i] )
i++;
offset = block*(long)buffsize + i;
error = -6;
break;
}
block++;
}
}
close(fh1);
close(fh2);
return ( error );
}